﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel.Design;
using ProgNetComponents.Controls;
using System.Drawing;
using ProgNetComponents.Interfaces;
using ProgNetComponents.Classes;
using ProgNetComponents.Collections;
using DevExpress.Utils;
using ProgNetComponentsDx.Design.Designers;

namespace ProgNetComponentsDx.Shortcut.Design
{
    public class ShortcutGroupCollectionEditorForm : BaseGroupCollectionDesigner
    {
        private TextBox txtEditor;
        private Label lblValue;
        private Button btnClear;
    
        public ShortcutGroupCollectionEditorForm()
        {
            InitializeComponent();
        }
        
        private void InitializeComponent(){
            this.btnClear = new System.Windows.Forms.Button();
            this.txtEditor = new System.Windows.Forms.TextBox();
            this.lblValue = new System.Windows.Forms.Label();
            this.panel2.SuspendLayout();
            this.panel1.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).BeginInit();
            this.splitContainer1.Panel1.SuspendLayout();
            this.splitContainer1.Panel2.SuspendLayout();
            this.splitContainer1.SuspendLayout();
            this.SuspendLayout();
            // 
            // treeView1
            // 
            this.treeView1.LineColor = System.Drawing.Color.Black;
            this.treeView1.Size = new System.Drawing.Size(204, 224);
            // 
            // label1
            // 
            this.label1.Size = new System.Drawing.Size(204, 20);
            // 
            // propertyGrid1
            // 
            this.propertyGrid1.Size = new System.Drawing.Size(224, 298);
            // 
            // panel1
            // 
            this.panel1.Controls.Add(this.btnClear);
            this.panel1.Controls.Add(this.txtEditor);
            this.panel1.Controls.Add(this.lblValue);
            this.panel1.Size = new System.Drawing.Size(484, 52);
            this.panel1.TabIndex = 0;
            this.panel1.Controls.SetChildIndex(this.lblValue, 0);
            this.panel1.Controls.SetChildIndex(this.btnOk, 0);
            this.panel1.Controls.SetChildIndex(this.txtEditor, 0);
            this.panel1.Controls.SetChildIndex(this.btnCancel, 0);
            this.panel1.Controls.SetChildIndex(this.btnClear, 0);
            // 
            // btnCancel
            // 
            this.btnCancel.Location = new System.Drawing.Point(406, 17);
            // 
            // btnOk
            // 
            this.btnOk.Location = new System.Drawing.Point(325, 17);
            // 
            // splitContainer1
            // 
            this.splitContainer1.Size = new System.Drawing.Size(484, 298);
            this.splitContainer1.SplitterDistance = 204;
            // 
            // btnClear
            // 
            this.btnClear.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.btnClear.Location = new System.Drawing.Point(109, 27);
            this.btnClear.Name = "btnClear";
            this.btnClear.Size = new System.Drawing.Size(134, 23);
            this.btnClear.TabIndex = 12;
            this.btnClear.Text = "Clear Default";
            this.btnClear.UseVisualStyleBackColor = true;
            this.btnClear.Click += new System.EventHandler(this.btnClear_Click);
            // 
            // txtEditor
            // 
            this.txtEditor.Location = new System.Drawing.Point(108, 6);
            this.txtEditor.Name = "txtEditor";
            this.txtEditor.Size = new System.Drawing.Size(134, 20);
            this.txtEditor.TabIndex = 8;
            this.txtEditor.TextChanged += new System.EventHandler(this.txtEditor_TextChanged);
            this.txtEditor.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txtEditor_KeyDown);
            this.txtEditor.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.txtEditor_KeyPress);
            // 
            // lblValue
            // 
            this.lblValue.Location = new System.Drawing.Point(4, 5);
            this.lblValue.Name = "lblValue";
            this.lblValue.Size = new System.Drawing.Size(99, 20);
            this.lblValue.TabIndex = 11;
            this.lblValue.Text = "Default Shortcut:";
            this.lblValue.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // ShortcutGroupCollectionEditorForm
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.ClientSize = new System.Drawing.Size(484, 350);
            this.MinimumSize = new System.Drawing.Size(500, 350);
            this.Name = "ShortcutGroupCollectionEditorForm";
            this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Show;
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.panel2.ResumeLayout(false);
            this.panel1.ResumeLayout(false);
            this.panel1.PerformLayout();
            this.splitContainer1.Panel1.ResumeLayout(false);
            this.splitContainer1.Panel2.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).EndInit();
            this.splitContainer1.ResumeLayout(false);
            this.ResumeLayout(false);

        }

        protected override ProgNetComponents.Interfaces.INamedGroup CreateNewGroup()
        {
            return new ShortcutGroup();
        }

        protected override ProgNetComponents.Interfaces.INamedObject CreateNewItem()
        {
            Shortcut s = new Shortcut();
            if (ShortcutValue != null)
                s.DefaultShortcut = ShortcutValue.Key;
            return s;
        }

        private KeyShortcut _ShortcutValue = new KeyShortcut();
        public KeyShortcut ShortcutValue
        {
            get
            {
                return _ShortcutValue;
            }
            set
            {
                _ShortcutValue = value;
                SetText();
            }
        }
        
        private void SetText()
        {
            if (_ShortcutValue.IsExist)
                txtEditor.Text = _ShortcutValue.ToString();
            else
                txtEditor.Text = "";
            txtEditor.SelectionStart = txtEditor.Text.Length;
        }

        private bool IsFitKey(KeyEventArgs e)
        {
            if (e.KeyCode == (Keys.LButton | Keys.ShiftKey) || e.KeyCode == Keys.ShiftKey || (e.KeyCode == (Keys.RButton | Keys.ShiftKey) || e.KeyCode == (Keys.MButton | Keys.ShiftKey)))
                return false;
            else if (e.KeyData == Keys.Back)
            {
                ShortcutValue = new KeyShortcut(ShortcutValue.Key);
                SetText();
                return false;
            }
            else if (e.KeyData == (Keys.LButton | Keys.MButton | Keys.Back))
            {
                return false;
            }
            else if (e.KeyData == (Keys.LButton | Keys.RButton | Keys.Back | Keys.ShiftKey))
            {
                return false;
            }
            else if (e.KeyCode >= (Keys.ShiftKey | Keys.Space) && (e.KeyCode <= (Keys)90 && !e.Alt) && !e.Control)
                return false;
            else
                return true;
        }

        private void txtEditor_KeyDown(object sender, KeyEventArgs e)
        {
            e.Handled = true;
            if (IsFitKey(e))
            {
                ShortcutValue = new KeyShortcut(e.KeyData);
                SetText();
            }
        }

        private void txtEditor_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = true;
        }

        private void txtEditor_TextChanged(object sender, EventArgs e)
        {
            SetText();
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            _ShortcutValue = new KeyShortcut();
            SetText();
        }
    }
}
